package com.znh.kuiklyuniversity.compose

import androidx.annotation.RestrictTo
import androidx.compose.runtime.Composable
import androidx.compose.runtime.IntState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import com.tencent.kuikly.compose.ComposeContainer
import com.tencent.kuikly.compose.animation.core.spring
import com.tencent.kuikly.compose.foundation.Image
import com.tencent.kuikly.compose.foundation.layout.Box
import com.tencent.kuikly.compose.foundation.layout.Column
import com.tencent.kuikly.compose.foundation.layout.fillMaxSize
import com.tencent.kuikly.compose.foundation.pager.HorizontalPager
import com.tencent.kuikly.compose.foundation.pager.PageSize
import com.tencent.kuikly.compose.foundation.pager.PagerState
import com.tencent.kuikly.compose.foundation.pager.VerticalPager
import com.tencent.kuikly.compose.foundation.pager.rememberPagerState
import com.tencent.kuikly.compose.material3.Text
import com.tencent.kuikly.compose.resources.DrawableResource
import com.tencent.kuikly.compose.resources.InternalResourceApi
import com.tencent.kuikly.compose.resources.painterResource
import com.tencent.kuikly.compose.setContent
import com.tencent.kuikly.compose.ui.Alignment
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.graphics.Color
import com.tencent.kuikly.compose.ui.layout.ContentScale
import com.tencent.kuikly.compose.ui.layout.onSizeChanged
import com.tencent.kuikly.compose.ui.platform.LocalFocusManager
import com.tencent.kuikly.compose.ui.unit.IntSize
import com.tencent.kuikly.core.annotations.Page
import com.tencent.kuikly.core.base.Scale
import com.tencent.kuikly.core.base.attr.ImageUri
import com.tencent.kuikly.core.log.KLog
import com.tencent.kuikly.core.module.Module
import com.tencent.kuikly.core.nvi.serialization.json.JSONObject
import com.tencent.kuikly.core.nvi.serialization.json.JSONStringer
import com.tencent.kuikly.core.pager.IPagerEventObserver
import com.tencent.kuikly.core.pager.Pager
import com.znh.kuiklyuniversity.backgroundColor
import com.znh.kuiklyuniversity.compose.entrys.TabsMenu
import com.znh.kuiklyuniversity.compose.mainPage.ListModel
import com.znh.kuiklyuniversity.compose.mainPage.ListPage
import com.znh.kuiklyuniversity.compose.mainPage.MyPage
import com.znh.kuiklyuniversity.compose.widgets.BottonLayout
import com.znh.kuiklyuniversity.compose.mainPage.RobotPage
import com.znh.kuiklyuniversity.compose.mainPage.StorePage
import com.znh.kuiklyuniversity.compose.model.ToNativalModel
import com.znh.kuiklyuniversity.compose.widgets.BottonController
import com.znh.kuiklyuniversity.compose.widgets.TitleLayout
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Page("ComposeRoot", supportInLocal = true)
internal class RouterPageView : ComposeContainer(), IPagerEventObserver {

    override fun willInit() {
        super.willInit()
        setContent {
            RouterPageContent()
        }
    }


    override fun onCreatePager(pagerId: String, pageData: JSONObject) {
        super.onCreatePager(pagerId, pageData)

    }

    override fun onDestroyPager() {
        super.onDestroyPager()
    }

    override fun onPagerEvent(pagerEvent: String, eventData: JSONObject) {
        // 3. 处理页面事件
    }
//    getPager().addPagerEventObserver(this) // 2. 监听页面事件
//    getPager().removePagerEventObserver(this) // 4. 取消页面事件监听, 防止内存泄漏


    override fun onReceivePagerEvent(pagerEvent: String, eventData: JSONObject) {
        super.onReceivePagerEvent(pagerEvent, eventData)
        // pagerEvent: 事件名字
        // eventData: 事件数据
        KLog.i("znh","Kuikly接收到："+eventData.toString())
    }

    @OptIn(InternalResourceApi::class)
    @Composable
    fun RouterPageContent() {

        var cutIndex: Int by remember { mutableStateOf(0) }
        val pageCount = 4
        val pagerState = rememberPagerState(
            initialPage = 0,
            pageCount = { pageCount }
        )
        val coroutineScope = rememberCoroutineScope()
        var btnController: BottonController = BottonController()
        var menuList: List<TabsMenu> = listOf(
            TabsMenu("首页", "icon/ic_action_android.png", "icon/ic_action_android.png", ac = {
                KLog.i("znh", "---------aciton-----" + cutIndex)
                if (cutIndex == 0) {
                    return@TabsMenu
                }
                cutIndex = 0
                coroutineScope.launch {
                    pagerState.animateScrollToPage(page = cutIndex)
                }
            }),
            TabsMenu("商场", "icon/ic_action_document.png", "icon/ic_action_document.png", ac = {
                if (cutIndex == 1) {
                    return@TabsMenu
                }
                cutIndex = 1
                coroutineScope.launch {
                    pagerState.animateScrollToPage(page = cutIndex)
                }
            }),
            TabsMenu("购物车", "icon/ic_action_paste.png", "icon/ic_action_paste.png", ac = {
                if (cutIndex == 2) {
                    return@TabsMenu
                }
                cutIndex = 2
                coroutineScope.launch {
                    pagerState.animateScrollToPage(page = cutIndex)
                }
            }),
            TabsMenu("我的", "icon/ic_action_user.png", "icon/ic_action_user.png", ac = {
                if (cutIndex == 3) {
                    return@TabsMenu
                }
                cutIndex = 3
                coroutineScope.launch {
                    pagerState.animateScrollToPage(page = cutIndex)
                }
            })
        )


        val focusManager = LocalFocusManager.current

            Column(
                modifier = Modifier.fillMaxSize()
                    .backgroundColor(Color(230, 230, 230))
                    .onSizeChanged { intSize: IntSize ->

                    }
            ) {
                Column(modifier = Modifier.weight(1f)) {
                    HorizontalPager(
                        pageSize = PageSize.Fill,
                        state = pagerState,
                        pageContent = { index ->
                            cutIndex = index
                            btnController.selectItem(index)
                            KLog.i("znh", "---- cuttent page index=" + index)
                            pageContentInfo(index, this@RouterPageView)
                        }
                    )
                }
                BottonLayout(menuList = menuList, controller = btnController)
            }


    }


    override fun createExternalModules(): Map<String, Module>? {
        return mapOf(
            "NativalModule" to ToNativalModel()
        )
    }

}



@Composable
fun pageContentInfo(index: Int, ct: ComposeContainer) {
//   var indexChange: Int by remember { mutableStateOf(index) }
    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        if (index == 0) {
            RobotPage(ct)
        } else if (index == 1) {
            StorePage()
        } else if (index == 2) {
            ListPage()
        } else if (index == 3) {
            MyPage(ct)
        } else {
            Text("暂未开启" + index, color = Color.Black)
        }

    }
}
